package com.kld.data.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.kld.common.annotation.Log;
import com.kld.common.core.controller.BaseController;
import com.kld.common.core.domain.PageQuery;
import com.kld.common.core.domain.R;
import com.kld.common.core.page.TableDataInfo;
import com.kld.common.enums.BusinessType;
import com.kld.data.domain.po.SysTrans;
import com.kld.data.service.ISysTransService;
import com.kld.framework.web.domain.AjaxResult;
import io.swagger.annotations.Api;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 数据转换任务Controller
 *
 * @author ruoyi
 * @date 2020-12-03
 */
@Validated
@Api(value = "数据转换控制器", tags = {"数据转换控制器"})
@RequiredArgsConstructor
@RestController
@RequestMapping("/data/trans")
public class SysTransController extends BaseController {

    private final ISysTransService sysTransService;

//    @Value("${webservice.datasharing.self-url}")
//    private String selfUrl;

//    @Autowired
//    private MongoTemplate mongoTemplate;


//    /**
//     * 执行自助服务数据转换任务
//     */
//    @Log(title = "执行自助服务数据转换任务", businessType = BusinessType.INSERT)
//    @PostMapping("/doSelfTrans")
//    public AjaxResult doSelfTrans(@RequestBody SysTrans sysTrans) {
//        int count = sysTransService.executeTransTask(sysTrans);//执行数据转换任务
//        if (count == 0) {//数据转换成功
//            sysTransService.insertSysTrans(sysTrans);//新增数据转换数据
//            JSONObject obj = new JSONObject();
//            obj.put("tableName", sysTrans.getTableName());
//            obj.put("tableStatus", "2");
//            HttpUtils.sendJsonPost(selfUrl, obj);//回调修改搜索引擎记录表
//        } else {//数据转换失败
//            JSONObject obj = new JSONObject();
//            obj.put("tableName", sysTrans.getTableName());
//            obj.put("tableStatus", "0");
//            HttpUtils.sendJsonPost(selfUrl, obj);//回调修改搜索引擎记录表
//        }
//        return AjaxResult.success();
//    }

    /**
     * 查询数据转换任务列表
     *
     * @return
     */
    @SaCheckPermission("data:trans:list")
    @GetMapping("/list")
    public TableDataInfo list(SysTrans sysTrans, PageQuery pageQuery) {
        return sysTransService.selectSysTransList(sysTrans,pageQuery);
    }


    /**
     * 获取数据转换任务详细信息
     */
    @SaCheckPermission("data:trans:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(sysTransService.selectSysTransById(id));
    }

    /**
     * 新增数据转换任务
     */
    @SaCheckPermission("data:trans:add")
    @Log(title = "数据转换任务", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@RequestBody SysTrans sysTrans) {
        return toAjax(sysTransService.insertSysTrans(sysTrans));
    }

    /**
     * 修改数据转换任务
     */
    @SaCheckPermission("data:trans:edit")
    @Log(title = "数据转换任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody SysTrans sysTrans) {
        return toAjax(sysTransService.updateSysTrans(sysTrans));
    }

    /**
     * 删除数据转换任务
     */
    @SaCheckPermission("data:trans:remove")
    @Log(title = "数据转换任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@PathVariable String[] ids) {
        return toAjax(sysTransService.deleteSysTransByIds(ids));
    }

    /**
     * 运行一次数据转换任务
     */
    @SaCheckPermission("data:trans:remove")
    @Log(title = "数据转换任务", businessType = BusinessType.START)
    @PostMapping("/start")
    public R<Void> start(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        SysTrans systrans = sysTransService.selectSysTransById(id);
        return toAjax(sysTransService.executeTransTask(systrans));
    }

    /**
     * 启动/停止数据转换任务
     */
    @SaCheckPermission("data:trans:remove")
    @Log(title = "数据转换任务", businessType = BusinessType.STOP)
    @PostMapping("/change")
    public R<Void> change(@RequestBody Map<String, String> params) {
        String id = params.get("id");
        String status = params.get("status");
        SysTrans systrans = sysTransService.selectSysTransById(id);
        systrans.setTaskStatus(status);
        return toAjax(sysTransService.changeTaskStatus(systrans));
    }

}
